package com.hk.rabbitmq.one;

import com.rabbitmq.client.*;

/**
 * @author: dpb
 * @date: 2022/3/9
 */
public class Consumer {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args) throws Exception{
        ConnectionFactory factory = new ConnectionFactory();

        factory.setHost("121.40.45.96");
        factory.setUsername("admin");
        factory.setPassword("123");

        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        System.out.println("等待接收消息..........");
        //推送的消息如何进行消费的接口调用
        DeliverCallback deliverCallback = ((consumerTag, delivery) -> {
            String message = new String(delivery.getBody());
            System.out.println(message);
        });

        //取消消费的一个回调接口 队列被删除掉了
        CancelCallback cancelCallback = (consumerTag -> {
            System.out.println("消息消费被中断");
        });

        /**
         *
         */
        channel.basicConsume(QUEUE_NAME, true, deliverCallback, cancelCallback);
    }
}
